--插入数据触发函数
DROP TRIGGER IF EXISTS trace_insert ON gs_temporary_data;
DROP FUNCTION IF EXISTS record_data_trace_insert();
CREATE OR REPLACE FUNCTION record_data_trace_insert()
RETURNS TRIGGER AS $BODY$
BEGIN
      INSERT INTO sm_data_trace(
        data_id,
        gis_id,
        pre_geom,
        geo_layer,
        is_new,
        sync_private,
        sync_public,
        is_checked,
        delete_data,
        update_time,
        create_time)
      VALUES(
        NEW.data_id,
        NEW.gis_id,
        NEW.geom,
        NEW.geo_layer,
        TRUE,
        FALSE,
        FALSE,
        FALSE,
        FALSE,
        now(),
        now());
RETURN NULL;
END;
$BODY$ LANGUAGE plpgsql;

--插入数据触发器
CREATE TRIGGER trace_insert AFTER INSERT ON gs_temporary_data FOR EACH ROW
EXECUTE PROCEDURE record_data_trace_insert();



--编辑数据触发函数
DROP TRIGGER IF EXISTS trace_update ON gs_temporary_data;
DROP FUNCTION IF EXISTS record_data_trace_update();
CREATE OR REPLACE FUNCTION record_data_trace_update()
RETURNS TRIGGER AS $BODY$
BEGIN
  IF st_asewkt(NEW.geom) != st_asewkt(OLD.geom) THEN
      IF EXISTS (SELECT gis_id FROM sm_data_trace WHERE gis_id = NEW.gis_id AND sync_public = FALSE) THEN
        UPDATE sm_data_trace SET
          is_checked = FALSE,
          sync_private = FALSE,
          update_time = now() WHERE gis_id = NEW.gis_id AND sync_public = FALSE;
      ELSE
        INSERT INTO sm_data_trace(
          data_id,
          gis_id,
          pre_geom,
          geo_layer,
          is_new,
          sync_private,
          sync_public,
          is_checked,
          delete_data,
          update_time,
          create_time)
        VALUES(
          NEW.data_id,
          NEW.gis_id,
          NEW.geom,
          NEW.geo_layer,
          FALSE,
          FALSE,
          FALSE,
          FALSE,
          FALSE,
          now(),
          now());
      END IF;
      UPDATE gs_temporary_data SET is_modify = TRUE WHERE data_id = NEW.data_id;
  END IF;

  IF OLD.is_deleted=FALSE and NEW.is_deleted=TRUE THEN
      IF EXISTS (SELECT gis_id FROM sm_data_trace WHERE gis_id = OLD.gis_id AND sync_public = FALSE) THEN
        UPDATE sm_data_trace SET
          delete_data = TRUE,
          is_checked = FALSE,
          sync_private = FALSE,
          update_time = now() WHERE gis_id = OLD.gis_id AND sync_public = FALSE;
      ELSE
        INSERT INTO sm_data_trace(
          data_id,
          gis_id,
          pre_geom,
          geo_layer,
          is_new,
          sync_private,
          sync_public,
          is_checked,
          delete_data,
          update_time,
          create_time)
        VALUES(
          OLD.data_id,
          OLD.gis_id,
          OLD.geom,
          OLD.geo_layer,
          FALSE,
          FALSE,
          FALSE,
          FALSE,
          TRUE,
          now(),
          now());
      END IF;
  END IF;

RETURN NULL;
END;
$BODY$ LANGUAGE plpgsql;

--编辑数据触发器
CREATE TRIGGER trace_update AFTER UPDATE ON gs_temporary_data FOR EACH ROW
EXECUTE PROCEDURE record_data_trace_update();


--删除数据触发函数
-- DROP TRIGGER IF EXISTS trace_delete ON gs_temporary_data;
-- DROP FUNCTION IF EXISTS record_data_trace_delete();
-- CREATE OR REPLACE FUNCTION record_data_trace_delete()
-- RETURNS TRIGGER AS $BODY$
-- BEGIN
--       IF EXISTS (SELECT gis_id FROM sm_data_trace WHERE gis_id = OLD.gis_id AND sync_public = FALSE) THEN
--         UPDATE sm_data_trace SET
--           delete_data = TRUE,
--           is_checked = FALSE,
--           sync_private = FALSE,
--           update_time = now() WHERE gis_id = OLD.gis_id AND sync_public = FALSE;
--       ELSE
--         INSERT INTO sm_data_trace(
--           data_id,
--           gis_id,
--           pre_geom,
--           geo_layer,
--           is_new,
--           sync_private,
--           sync_public,
--           is_checked,
--           delete_data,
--           update_time,
--           create_time)
--         VALUES(
--           OLD.data_id,
--           OLD.gis_id,
--           OLD.geom,
--           OLD.geo_layer,
--           FALSE,
--           FALSE,
--           FALSE,
--           FALSE,
--           TRUE,
--           now(),
--           now());
--       END IF;
-- RETURN NULL;
-- END;
-- $BODY$ LANGUAGE plpgsql;

--删除数据触发器
-- CREATE TRIGGER trace_delete AFTER DELETE ON gs_temporary_data FOR EACH ROW
-- EXECUTE PROCEDURE record_data_trace_delete();



--审核数据触发函数
DROP TRIGGER IF EXISTS trace_audit_update ON sm_task_data_relation;
DROP FUNCTION IF EXISTS record_data_trace_audit_update();
CREATE OR REPLACE FUNCTION record_data_trace_audit_update()
RETURNS TRIGGER AS $BODY$
BEGIN
  IF NEW.audit_status != OLD.audit_status THEN
      IF EXISTS (SELECT data_id FROM sm_data_trace WHERE data_id = NEW.data_id AND sync_public = FALSE) THEN
        UPDATE sm_data_trace SET
          is_checked = TRUE,
          update_time = now() WHERE data_id = NEW.data_id AND sync_public = FALSE;
      END IF;
      UPDATE gs_temporary_data SET is_modify = FALSE WHERE data_id = NEW.data_id;
  END IF;
  IF NEW.async_status = 107030::BIGINT THEN
      IF EXISTS (SELECT data_id FROM sm_data_trace WHERE data_id = NEW.data_id AND sync_public = FALSE) THEN
        UPDATE sm_data_trace SET
          sync_public = TRUE,
          update_time = now() WHERE data_id = NEW.data_id AND sync_public = FALSE;
      END IF;
  END IF;
RETURN NULL;
END;
$BODY$ LANGUAGE plpgsql;

--审核数据触发器
CREATE TRIGGER trace_audit_update AFTER UPDATE ON sm_task_data_relation FOR EACH ROW
EXECUTE PROCEDURE record_data_trace_audit_update();